package com.supermap.qdsearch.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.supermap.qdsearch.R;
import com.supermap.qdsearch.entity.CollectBean;
import com.supermap.qdsearch.utils.ConstUtils;
import com.supermap.qdsearch.widget.CustomListView;
import com.supermap.qdsearch.widget.SwipeLayout;
import com.supermap.qdsearch.widget.SwipeLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XueQing Wang on 2018/6/4.
 */

public class SwipeAdapter extends BaseAdapter {

    private List<CollectBean> list;
    private Context context;
    private int state;


    public SwipeAdapter(List<CollectBean> list, Context context, int state) {
        this.list = list;
        this.context = context;
        this.state = state;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.swipe_layout, null);
            holder = new ViewHolder(view);
            convertView = view;
            view.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        //隐藏菜单
        showAndHiddenMenu(holder);

        //解决多次调用getView方法  利用当OnLayout结束
        if (parent instanceof CustomListView) {
            if (((CustomListView) parent).isOnMeasure()) {
                return convertView;
            }
        }

        CollectBean bean = list.get(position);

        holder.tvInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onInfoClick(position);
                }
            }
        });

        holder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onEditClick(position);
                }
            }
        });

        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onDeleteClick(position);
                }
            }
        });
        holder.tvCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onCheckClick(position);
                }
            }
        });
        holder.tvRepair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onRepairClick(position);
                }
            }
        });
        holder.tvVerify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onVerifyClick(position);
                }
            }
        });
        return convertView;
    }

    public void showAndHiddenMenu(ViewHolder holder) {
        if (state == ConstUtils.COLLECT_STATE) {
            holder.tvCheck.setVisibility(View.GONE);
            holder.tvRepair.setVisibility(View.GONE);
            holder.tvVerify.setVisibility(View.GONE);

            holder.tvInfo.setVisibility(View.GONE);
            holder.tvEdit.setVisibility(View.VISIBLE);
            holder.tvDelete.setVisibility(View.VISIBLE);
        } else if (state == ConstUtils.SEARCH_STATE) {
            holder.tvCheck.setVisibility(View.VISIBLE);
            holder.tvRepair.setVisibility(View.GONE);
            holder.tvVerify.setVisibility(View.GONE);

            holder.tvInfo.setVisibility(View.GONE);
            holder.tvEdit.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);
        } else if (state == ConstUtils.REPAIR_STATE) {
            holder.tvCheck.setVisibility(View.GONE);
            holder.tvRepair.setVisibility(View.VISIBLE);
            holder.tvVerify.setVisibility(View.GONE);

            holder.tvInfo.setVisibility(View.GONE);
            holder.tvEdit.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);

        } else if (state == ConstUtils.VERIFY_STATE) {
            holder.tvCheck.setVisibility(View.GONE);
            holder.tvRepair.setVisibility(View.GONE);
            holder.tvVerify.setVisibility(View.VISIBLE);

            holder.tvInfo.setVisibility(View.GONE);
            holder.tvEdit.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);
        }
    }


    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {

        public void onCheckClick(int position);

        public void onRepairClick(int position);

        public void onVerifyClick(int position);

        public void onInfoClick(int position);

        public void onEditClick(int position);

        public void onDeleteClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.tv_id)
        TextView tvId;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_build)
        TextView tvBuild;
        @BindView(R.id.tv_floor)
        TextView tvFloor;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_info)
        TextView tvInfo;
        @BindView(R.id.tv_check)
        TextView tvCheck;
        @BindView(R.id.tv_repair)
        TextView tvRepair;
        @BindView(R.id.tv_verify)
        TextView tvVerify;
        @BindView(R.id.tv_edit)
        TextView tvEdit;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.swipeLayout)
        SwipeLayout swipeLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
